﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BarberShop.Model;
using BarberShop.Data;
using System.Linq.Expressions;
using BarberShop.Common;

namespace BarberShop.BLL
{
    public class MoviePalyListBLL
    {
        
        public static IList<MovePlayListInfo> GetMoviePalyList(string name)
        { 
            using(DataClassesDataContext data = new DataClassesDataContext())
            {
                Expression<Func<MovePlayListInfo, bool>> WhereExpr = PredicateExtensions.True<MovePlayListInfo>();
                if (!string.IsNullOrEmpty(name))
                {
                    WhereExpr = WhereExpr.And(x => x.Name.Contains(name));
                }
                var Move = data.MovePlayListInfos.Where(WhereExpr);
                return Move.ToList();
            }
        }

        public static IList<View_MovePlayListModel> GetViewMoviePalyList(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                //Func<View_MovePlayList, View_MovePlayList> make = a =>  new View_MovePlayList 
                //{
                //    CreateDate = Convert.ToDateTime(a.CreateDate),
                //    FileName = Convert.ToString(a.FileName),
                //    PlayListId = a.PlayListId,
                //    MoveName = Convert.ToString(a.MoveName),
                //    MovieId = a.MovieId,
                //    PlayIndex = a.PlayIndex,
                //    PlayListReId = a.PlayListReId,
                //    Size = a.Size.HasValue ? a.Size.Value : 0,
                //    TimeLength = a.TimeLength.HasValue ? a.TimeLength.Value : 0
                //};

                //return (from c in data.GetTable<View_MovePlayList>() where c.PlayListId == id select make(c)).ToList<View_MovePlayList>();

                var list = from a in data.View_MovePlayList
                           where a.PlayListId == id
                           select new View_MovePlayListModel
                           {
                               CreateDate =a.CreateDate.HasValue?a.CreateDate.Value:DateTime.Now,
                               FileName = Convert.ToString(a.FileName),
                               PlayListId = a.PlayListId,
                               MoveName = Convert.ToString(a.MoveName),
                               MovieId = a.MovieId,
                               PlayIndex = a.PlayIndex,
                               PlayListReId = a.PlayListReId,
                               Size = a.Size.HasValue ? a.Size.Value : 0,
                               TimeLength = a.TimeLength.HasValue ? a.TimeLength.Value : 0
                           };

                return list.ToList();
            }
        }
        /// <summary>
        /// 添加影片
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool AddMovePlayListInfo(MovePlayListInfo info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    data.MovePlayListInfos.InsertOnSubmit(info);
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }

        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static MovePlayListInfo GetMovePlayListInfo(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var info = (from a in data.MovePlayListInfos where a.ID == id select a).FirstOrDefault();
                return info;
            }
        }

        public static bool UpdateMovePlayListInfo(MovePlayListInfo info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    var r = (from a in data.MovePlayListInfos where a.ID == info.ID select a).FirstOrDefault();
                    r.CreateDate = info.CreateDate;
                    r.ID = info.ID;
                    r.Name = info.Name;
                    r.Status = info.Status;
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }
        }

        /// <summary>
        /// 获取播放列表总大小
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static double GetTotalSize(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var list = (from a in data.MovePlayListRelation where a.MovePlayListInfo_ID == id select a).ToList();
                double size = 0;
                foreach (var item in list)
                {
                    var info=MovieInfoBLL.GetMoveInfo(item.MoveInfo_ID.Value);
                    size += info.Size.Value;
                }
                return size;
            }
        }

        /// <summary>
        /// 获取播放列表总时长
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int GetTotalTimeLength(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var list = (from a in data.MovePlayListRelation where a.MovePlayListInfo_ID == id select a).ToList();
                int times = 0;
                foreach (var item in list)
                {
                    var info = MovieInfoBLL.GetMoveInfo(item.MoveInfo_ID.Value);
                    times += info.TimeLength.Value;
                }
                return times;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="playid"></param>
        /// <param name="playreid"></param>
        /// <param name="newplayindex"></param>
        /// <param name="oldplayindex"></param>
        public static void ModifyPlayIndex(int playid, int playreid, int newplayindex, int oldplayindex)
        {
            if (newplayindex < oldplayindex&&newplayindex>0)
            {
                using (DataClassesDataContext data = new DataClassesDataContext())
                {
                    var list = (from a in data.MovePlayListRelation where a.MovePlayListInfo_ID == playid &&a.PlayIndex<oldplayindex&&a.PlayIndex>=newplayindex select a).ToList();
                    foreach (var item in list)
                    {
                        item.PlayIndex = item.PlayIndex + 1;
                    }
                    var info = (from a in data.MovePlayListRelation where a.ID == playreid select a).FirstOrDefault();
                    info.PlayIndex = newplayindex;
                    data.SubmitChanges();
                }
            }
            if (newplayindex > oldplayindex)
            {
                using (DataClassesDataContext data = new DataClassesDataContext())
                {
                    var list = (from a in data.MovePlayListRelation where a.MovePlayListInfo_ID == playid && a.PlayIndex > oldplayindex && a.PlayIndex <= newplayindex select a).ToList();
                    foreach (var item in list)
                    {
                        item.PlayIndex = item.PlayIndex - 1;
                    }
                    var info2 = (from a in data.MovePlayListRelation where a.ID == playreid select a).FirstOrDefault();
                    info2.PlayIndex = newplayindex;
                    data.SubmitChanges();
                }
            }
        }

        public static void UpdatePlayIndex(int playid, int delPlayindex)
        {
           
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var list = (from a in data.MovePlayListRelation where a.MovePlayListInfo_ID == playid && a.PlayIndex>delPlayindex select a).ToList();
                foreach (var item in list)
                {
                    item.PlayIndex = item.PlayIndex - 1;
                }
                data.SubmitChanges();
            }
        }
    }
}
